<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class WechatController extends Controller
{
    /**
     * 处理微信的请求消息
     * @return string
     */
    public function serve()
    {
        $wechat = app('wechat');
        $userApi = $wechat->user;
        $wechat->server->setMessageHandler(function($message) use ($userApi, $wechat) {
            switch ($message->MsgType) {
                case 'event':
                    switch ($message->Event) {
                        case 'subscribe':
                            return '亲，终于等到您！感谢您关注天恒物业，如需服务帮助，请您拨打客服热线：0772-5519996，我们将竭诚为您服务!';
                            break;
                        default:
                            return '元宝尚城物业管理处服务电话：0772-5519996; 地址：元宝尚城一期一栋南门';
                            break;
                    }
                    break;
                case 'text':
                    return '你好！' . $userApi->get( $message->FromUserName)->nickname;
                    break;
                case 'image':
                    $image = new Image(['media_id'=>"DZ6axLbwLAHSICnxIH4wCV5bjTlGEKMOd3MLVdhR8Ew"
                    ]);
                    $wechat->staff->message($image)->to($message->FromUserName)->send();
                    return '你发送了图片，回复一张给你，哈哈！';
                    break;
                case 'voice':
                    return '收到语音消息';
                    break;
                case 'video':
                    return '收到视频消息';
                    break;
                case 'location':
                    return '收到坐标消息';
                    break;
                case 'link':
                    return '收到链接消息';
                    break;
                // ... 其它消息
                default:
                    return '收到其它消息';
                    break;
            }
        });
        return $wechat->server->serve();
    }

}
